package com.happe.foodeasy.controller.base;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.happe.foodeasy.dto.CustomerDTO;
import com.happe.foodeasy.normal.entity.CustomerEntity;
import com.happe.foodeasy.helper.GenerateAuthImage;
import com.happe.foodeasy.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * this class is used to response user register's request.
 * Created by wubin on 2016-04-03.
 */
@Controller
@RequestMapping("/register")
public class UserRegisterController {

    private CustomerService customerService;

    @Autowired
    public UserRegisterController(CustomerService customerService){
        Assert.notNull(customerService, "customerService must not null");
        this.customerService = customerService;
    }

    /**
     * when all customer's information are correct, will commit to here, this method is
     * used to store customer's information, and then skip to index page.
     * @param customerDTO use this entity to store the information that be sent by client.
     * @return
     */
    @RequestMapping(value = "/userRegister.do", method = RequestMethod.GET)
    @ResponseBody
    public String RegisterOneUser(/*@PathVariable String hello, @PathVariable String username,
                                  @MatrixVariable(required = false, defaultValue = "1") String user*/
                                  CustomerDTO customerDTO) {
        customerService.saveOneCustomer(CustomerEntity.newInstanceFromDTO(customerDTO));
        return "hello world";
    }

    /**
     * this method is used to check customer's username validity.
     * @param username the username that should be checked.
     * @return the result of verification.
     */
    @RequestMapping(value = "/checkUsername.do", method = RequestMethod.GET)
    @ResponseBody
    public String checkUsername(String username) {
        boolean checkResult = customerService.checkUserByName(username);
        String result = checkResult ? "succeed" : "defeat";

        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("result", result);
        Gson gson = new Gson();
        return gson.toJson(jsonObject);
    }

    /**
     * This method is used to verification code's image
     * @param session store the core in session
     * @param response write the image by byte array
     */
    @RequestMapping(value = "/obtainAuthImage.do", method = RequestMethod.GET)
    public void getVerificationCode(HttpSession session, HttpServletResponse response) {
        GenerateAuthImage gi = new GenerateAuthImage();
        byte[] resultByteImg = gi.generateImage();
        session.setAttribute("generateResult", gi.getRandomResult());
        response.setContentType("image/jpeg");
        try {
            response.getOutputStream().write(resultByteImg);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * This method is used to verification code's real result
     * @param session the code is stored in session
     * @return the result of core
     */
    @RequestMapping(value = "/obtainAuthResult.do", method = RequestMethod.GET)
    @ResponseBody
    public String getVerificationResult(HttpSession session) {
        String result = (String) session.getAttribute("generateResult");
        JsonObject jo = new JsonObject();
        jo.addProperty("result", result);
        Gson gson = new Gson();
        return gson.toJson(jo);
    }
}
